// book list controller
var app = angular.module('app');

app.controller('bookListCtrl', ['$scope', '$state', '$translate', '$window',
    'bookService', 'confirmDialogService', 'CONSTANTS', 'toasterService', 'errorUtilsService',
    function ($scope, $state, $translate, $window,
              bookService, confirmDialogService, CONSTANTS, toasterService, errorUtilsService) {

        $scope.currentPage = 1;
        $scope.pageButtonNum = CONSTANTS.book.pageButtonNum;
        $scope.addBook = function () {
            $state.go('app.book.add');
        };
        $scope.addBookFromNet = function () {
            $state.go('app.book.addFromNet');
        };
        $scope.checkQuery = function (query) {
            if (!query) {
                return;
            }
            if (query.isbn10 && query.isbn10 !== '') {
                if (query.isbn10.trim().length !== 10) {
                    // length must be 10
                    return $translate.instant('strings.book.list.query.isbn10LengthError');
                }
            }
            if (query.isbn13 && query.isbn13 !== '') {
                if (query.isbn13.trim().length !== 13) {
                    // length must be 10
                    return $translate.instant('strings.book.list.query.isbn13LengthError');
                }
            }

            return;
        };
        $scope.search = function (query, page) {
            var errMsg = $scope.checkQuery(query);
            if (errMsg) {
                toasterService.pop('error', null, errMsg);
                return;
            }
            bookService.list(query, page).then(function (rsp) {
                // suc
                $scope.response = rsp;
                $scope.currentPage = rsp.currentPage;
                // $scope.totalCount = rsp.totalCount;
                // $scope.pageCount = rsp.pageCount;
                $scope.perPageCount = rsp.perPageCount;
                if (rsp.isLast) {
                    $scope.totalCount = rsp.perPageCount; // last page, next page button is disabled.
                } else {
                    $scope.totalCount = rsp.perPageCount + 1; // make sure next page button is enabled.

                }

            }, function (err) {
                toasterService.pop('error', null, $translate.instant('strings.book.list.failure'));
            });
        };
        $scope.updateBook = function (index) {
            $state.go('app.book.update', {
                id: $scope.response.data[index]._id
            });
        };
        $scope.delBook = function (index) {
            confirmDialogService.showDialog($translate.instant('strings.dialog.title'),
                $translate.instant('strings.book.delete.tip'), 'sm')
                .then(function () {
                    return bookService.delete($scope.response.data[index]);
                })
                .then(function () {
                    return homepageOrderService.removeAll($scope.response.data[index]._id);
                })
                .then(function () {
                    return hotService.reduceAll($scope.response.data[index]._id);
                })
                .then(function (data) {
                    // delete suc
                    toasterService.pop('success', null, $translate.instant('strings.book.delete.success'));
                    $scope.response.data.splice(index, 1);
                })
                .catch(function (error) {
                    if (error) {
                        toasterService.pop('error', null,
                            errorUtilsService.parseErrorMsg(err, $translate.instant('strings.book.delete.failure')));
                    }
                });
        };

        $scope.pageChanged = function () {
            $scope.search($scope.query, $scope.currentPage);
        };

        // search books
        $scope.search($scope.query, $scope.currentPage);
    }]);